<template>
	<view v-if="state==0">
		<image class="header-img" :src="bgImageUrl" mode="widthFix"></image>
		<view class="auth-info padding_30">
			<view class="finish" v-if="userstore.authStatusInfo.company==0||userstore.authStatusInfo.company==1">
				已完成企业认证
			</view>
			<view class="title" v-else>
				填写资料
			</view>
			<view class="input-item">
				<view class="input-title">
					商家名称
				</view>
				<view class="input-box">
					<input placeholder-class="Placeholder" class="Input" style="width: 364rpx;" type="text" placeholder="请输入与营业执照相同的名称" v-model="userstore.companyAuthInfo.name" />
				</view>
			</view>
			<view class="input-item">
				<view class="input-title">
					统一社会信用编码
				</view>
				<view class="input-box">
					<input placeholder-class="Placeholder" class="Input" type="text" placeholder="请输入统一社会信用编码" v-model="userstore.companyAuthInfo.code" />
				</view>
			</view>
			<view class="input-item">
				<view class="input-title">
					商家类型
				</view>
				<view class="edit-info" @click="picker.open()">
					<!-- <picker mode="selector" @change="bindPickerChange" :value="index" :range="types" range-key="name">
						<view class="uni-input">{{}}</view>
					</picker> -->
					<view class="info">
						{{companyType?companyType:'请选择商家类型'}}
					</view>
					<image src="@/static/images/enter.png" mode="widthFix"></image>
				</view>
				<!-- <picker mode="selector" @change="bindPickerChange" :value="index" :range="types" range-key="name">
					<view class="uni-input" v-if="types[index]">{{types[index].name}}</view>
				</picker> -->
			</view>
			<view class="input-item">
				<view class="input-title">
					经营地址
				</view>
				<view class="input-box">
					<input placeholder-class="Placeholder" class="Input" type="text" placeholder="请输入经营地址" v-model="userstore.companyAuthInfo.address" />
				</view>
			</view>
			<view class="input-item">
				<view class="input-title">
					联系方式
				</view>
				<view class="input-box">
					<input placeholder-class="Placeholder" class="Input" type="number" placeholder="请输入联系方式" maxlength="11" v-model="userstore.companyAuthInfo.mobile" />
				</view>
			</view>
			<view class="title" v-if="userstore.authStatusInfo.company==0||userstore.authStatusInfo.company==1">
				营业执照正本
			</view>
			<view class="title" v-else>
				上传营业执照正本
				<text class="tips">(照片需保持正向,不能翻转倾斜)</text>
			</view>
			<view class="upload-box" @click="upload" v-if="userstore.companyAuthInfo.licenseimage">
				<image class="upload-image" :src="baseImgurl+'/'+userstore.companyAuthInfo.licenseimage" mode="aspectFill"></image>
			</view>
			<view class="upload-box" @click="upload" v-else>
				<image class="add" src="@/pages/user/static/images/add.png" mode="widthFix"></image>
			</view>
			<view class="title" v-if="userstore.authStatusInfo.company==0||userstore.authStatusInfo.company==1">
				授权证明（盖章版）
				<text class="tips">点击查看模版</text>
			</view>
			<view class="title" v-else>
				上传授权证明（盖章版）
				<!-- <text class="tips">(照片需保持正向,不能翻转倾斜)</text> -->
				<text class="tips">点击查看模版</text>
			</view>
			<view class="" style="display: flex;align-items: center;">
				<view class="upload-box" @click="workUpload" v-if="userstore.companyAuthInfo.workimage">
					<image class="upload-image" :src="baseImgurl+'/'+userstore.companyAuthInfo.workimage" mode="aspectFill">
					</image>
				</view>
				<view class="upload-box" @click="workUpload" v-else>
					<image class="add" src="@/pages/user/static/images/add.png" mode="widthFix"></image>
				</view>
				<view class="upload-box" style="width: 160rpx;height: 218rpx;margin-left: 30rpx;border: 1rpx solid #f5f5f5;" @click="previewImage(baseImgurl+'/'+priceTotal)">
					<image class="add" style="width: 100%;height:100%" :src="baseImgurl+'/'+priceTotal" mode="aspectFill"></image>
				</view>
			</view>
			<view class="btn" @click="auth" v-if="userstore.authStatusInfo.company!=0&&userstore.authStatusInfo.company!=1">
				立即认证
			</view>
		</view>

		<uv-picker ref="picker" title="商家类型" confirmColor="#FE3052" :columns="types" keyName="name" @confirm="confirm"></uv-picker>
	</view>
	<view class="auth-err-page flex" v-if="state==1">
		<view class="img-box">
			<image src="@/pages/user/static/images/auth_err.png" mode="widthFix"></image>
		</view>
		<view class="err-state">
			认证失败
		</view>
		<view class="err-tips">
			{{userstore.companyAuthInfo.reason}}
		</view>
		<view class="btn" @click="state=0">
			重新认证
		</view>
		<view class="return-btns" @click="returnBack">
			返回
		</view>
	</view>

</template>

<script setup>
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		nextTick,
		reactive,
		ref
	} from 'vue';
	import {
		getCurrentInstance
	} from "vue";
	const {
		proxy
	} = getCurrentInstance();
	import {
		throttle
	} from '@/utils/utils.js'
	import {
		useUserStore
	} from '@/store/modules/user.js'
	const userstore = useUserStore()
	import {
		useActiveStore
	} from '@/store/modules/active.js'
	const activeStore = useActiveStore()
	const types = ref([])
	const picker = ref()
	const state = ref(0)
	const baseImgurl = ref('')
	const companyType = ref('')
	const index = ref(0)
	const bgImageUrl = ref('')
	const priceTotal = ref('')
	onLoad(async () => {
		baseImgurl.value = uni.getStorageSync('baseUrl')
		if (userstore.authStatusInfo.company == 2) {
			// 认证失败
			state.value = 1
		}
		getPickerItem()
		getBannerList()
		getgetConFig()
	})
	// 获取banner图
	const getgetConFig = async () => {
		let res = await userstore.getConFig({
			type: 'authorizeTemplate'
		})
		if (res.code == 200) {
			priceTotal.value = res.data.content
		}
	}
	// 获取banner图
	const getBannerList = async () => {
		let res = await activeStore.getBannerList({
			position: 5
		})
		if (res.code == 200) {
			bgImageUrl.value = uni.getStorageSync('baseUrl') + res.data[0].image
		}
	}
	const previewImage = (image) => {
		uni.previewImage({
			current: image, // 当前显示图片的http链接
			urls: [image] // 需要预览的图片http链接列表
		});
	}
	// 获取商家类型picker
	const getPickerItem = async () => {
		let res = await userstore.getConFig({
			type: 'companyType'
		})
		types.value = [res.data.content]
		// 筛选出选择的type,通过type进行名字的赋值
		if (Object.keys(userstore.companyAuthInfo).length != 0) {
			let list = res.data.content.filter(item => {
				return item.id == userstore.companyAuthInfo.type
			})
			companyType.value = list[0].name
		}
	}
	const bindPickerChange = async (e) => {
		console.log(e)
		index.value = e.detail.value
	}
	const confirm = (e) => {
		userstore.companyAuthInfo.type = e.value[0].id
		companyType.value = e.value[0].name
	}
	// 营业执照上传
	const upload = async () => {
		let res = await proxy.$request.headportraits()
		userstore.companyAuthInfo.licenseimage = res
		uni.$uv.toast('上传成功')
	}
	// 工作证明上传
	const workUpload = async () => {
		let res = await proxy.$request.headportraits()
		userstore.companyAuthInfo.workimage = res
		uni.$uv.toast('上传成功')
	}
	// 立即认证
	const auth = throttle(async () => {
		let params = {
			check_type: 'company',
			name: userstore.companyAuthInfo.name,
			code: userstore.companyAuthInfo.code,
			type: userstore.companyAuthInfo.type,
			address: userstore.companyAuthInfo.address,
			mobile: userstore.companyAuthInfo.mobile,
			licenseimage: userstore.companyAuthInfo.licenseimage,
			workimage: userstore.companyAuthInfo.workimage,
			companyType: userstore.companyAuthInfo.companyType
		}
		let result = await userstore.editUserAuth(params)
		if (result.code == 200) {
			uni.$uv.toast('认证信息提交成功')
			setTimeout(() => {
				uni.navigateBack()
			}, 500)
		}
	}, 200)
	const returnBack = () => {
		uni.navigateBack()
	}
</script>

<style>
	page {
		background-color: #fff;
	}
</style>
<style lang="scss" scoped>
	// 认证失败
	.auth-err-page {
		flex-direction: column;

		.img-box {
			image {
				width: 402rpx;
			}
		}

		.err-state {
			padding: 60rpx 0 20rpx 0;
			font-weight: bold;
			font-size: 32rpx;
			color: #222222;
		}

		.err-tips {
			font-weight: 400;
			font-size: 28rpx;
			color: #666666;
			text-align: center;
		}

		.btn {
			margin-top: 100rpx;
			width: 512rpx;
		}

		.return-btns {
			margin-top: 60rpx;
			width: 512rpx;
			height: 100rpx;
			border-radius: 48rpx 48rpx 48rpx 48rpx;
			border: 2rpx solid #D8D8D8;
			font-weight: bold;
			font-size: 28rpx;
			color: #999999;
			line-height: 100rpx;
			text-align: center;
		}
	}

	.header-img {
		width: 100%;
	}

	.Placeholder {
		color: #999;
	}

	.auth-info {
		.title {
			font-weight: bold;
			font-size: 28rpx;
			color: #222222;

			.tips {
				font-weight: normal;
				color: #999999;
			}
		}

		.finish {
			font-weight: bold;
			font-size: 28rpx;
			color: #67C23A;
		}

		.upload-box {
			display: flex;
			align-items: center;
			justify-content: center;
			margin: 30rpx 0 60rpx 0;
			width: 218rpx;
			height: 218rpx;
			background: #F6F6F6;
			border-radius: 20rpx 20rpx 20rpx 20rpx;

			.add {
				width: 38rpx;
				height: 38rpx;
			}

			.upload-image {
				border-radius: 20rpx;
				width: 100%;
				height: 100%;
			}
		}

		.input-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 690rpx;
			height: 112rpx;
			margin: 30rpx auto;
			padding: 30rpx;
			background: #F6F6F6;
			border-radius: 20rpx 20rpx 20rpx 20rpx;

			.input-title {
				font-weight: 400;
				font-size: 28rpx;
				color: #222222;
			}

			.input-box {
				text-align: right;

				.Input {
					font-size: 28rpx;
				}
			}

			.state {
				font-weight: 400;
				font-size: 28rpx;
				color: #67C23A;
			}

			.edit-info {
				display: flex;
				align-items: center;

				.info {
					color: #999;
					text-align: right;
					font-size: 28rpx;

					image {
						width: 88rpx;
						height: 88rpx;
					}

					input {
						font-size: 28rpx;
					}
				}

				image {
					margin-left: 12rpx;
					width: 28rpx;
					height: 28rpx;
				}
			}
		}
	}

	.btn {}
</style>